﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using INSTA.StockApplication.BLL;

namespace INSTA.StockApplication.UI.Windows
{
    public partial class XtraForm1 : DevExpress.XtraEditors.XtraForm
    {
        private Orders commandes = new Orders();
        private Customer customer = new Customer();
        private int quantiteEntre = 0;

        public XtraForm1()
        {
            InitializeComponent();
            comboBox1.DataSource = customer.GetListOfUsersFromDatabase();
            comboBox1.ValueMember = "ID";
            comboBox1.DisplayMember = "NOM";


            var spinEditor = new RepositoryItemSpinEdit
                                 {
                                     MaxValue = 999999,
                                     MinValue = 0,
                                     IsFloatValue = false,
                                 };

            gridView1.BeforeLeaveRow += test;

            gridControl1.DataSource = null;
            gridControl1.MainView = gridView1;
            gridControl1.DataSource = commandes.GetProductsForOrdersFromDatabase();


            gridView1.Columns.Add();
            GridColumn totalColumn = gridView1.Columns.AddField("TOTAL_HT");
            totalColumn.VisibleIndex = 7;
            totalColumn.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
            totalColumn.OptionsColumn.AllowEdit = false;


            gridView1.Columns.Add();
            //Create an unbound column. 
            GridColumn unbColumn = gridView1.Columns.AddField("QUANTITE");
            unbColumn.VisibleIndex = 6;
            unbColumn.ColumnEdit = spinEditor;
            repositoryItemSpinEdit2.MinValue = 0;
            unbColumn.UnboundType = DevExpress.Data.UnboundColumnType.Integer;
            unbColumn.OptionsColumn.AllowEdit = true;

            gridView1.RefreshData();

        }

        private void test(object sender, RowAllowEventArgs e)
        {

        }

        private decimal GetTotalValueOfLine(GridView view, int listSourceRowIndex)
        {
            decimal quantite = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "QUANTITE"));
            decimal prix = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "PRIX_HT"));
            return quantite*prix;
        }

        private void XtraForm1_Load(object sender, EventArgs e)
        {

        }

        private void labelControl2_Click(object sender, EventArgs e)
        {

        }

        private void labelControl3_Click(object sender, EventArgs e)
        {

        }

        private void gridView1_ColumnUnboundExpressionChanged(object sender,
                                                              DevExpress.XtraGrid.Views.Base.ColumnEventArgs e)
        {

        }

        private void gridView1_CustomUnboundColumnData(object sender,
                                                       DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.Column == gridView1.Columns.ColumnByFieldName("QUANTITE") && e.IsSetData)
            {
                quantiteEntre = Convert.ToInt32(e.Value);
            }
            else if (e.Column == gridView1.Columns.ColumnByFieldName("QUANTITE") && e.IsGetData)
            {
                e.Value = quantiteEntre;
            }
            if (e.Column.FieldName == "TOTAL_HT" && e.IsGetData) 
                e.Value = GetTotalValueOfLine(gridView1, e.ListSourceRowIndex);
        }

        private void gridView1_RowCellClick(object sender, RowCellClickEventArgs e)
        {
        }

        private void gridView1_RowClick(object sender, RowClickEventArgs e)
        {
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
        }

        private void gridView1_Click(object sender, EventArgs e)
        {
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            bool verif = true;
            bool ajout = true;
            //CHECK STOCK
            for (int i = 0; i < gridView1.RowCount - 1; i++ )
            {
                if(Convert.ToInt32(gridView1.GetRowCellValue(i,"QUANTITE")) > Convert.ToInt32(gridView1.GetRowCellValue(i,"RESTANT")))
                {
                    verif = false;
                    break;
                }
            }
            if(verif)
            {
                //ajouter la commande
                int idCommande = commandes.AddOrderIntoDatabase(Convert.ToInt32(comboBox1.SelectedValue),
                                                                dateEdit1.DateTime);
                if (idCommande > 0)
                {
                    //commande enregistree,ajout lignes commande
                    for (int i = 0; i < gridView1.RowCount - 1; i++)
                    {
                        //produits ajoutes de la commande si quantite > 0

                        if(Convert.ToInt32(gridView1.GetRowCellValue(i,"QUANTITE")) > 0)
                        {
                            int idProduit = Convert.ToInt32(gridView1.GetRowCellValue(i, "ID_PRODUIT"));
                            int qte = Convert.ToInt32(gridView1.GetRowCellValue(i, "QUANTITE"));

                            if (!commandes.AddLinesOrderIntoDatabase(idCommande, idProduit, qte))
                            {
                                XtraMessageBox.Show("Erreur lors de l'enregistrement des lignes de la commande");
                                ajout = false;
                                break;
                            }
                        }

                    }
                }
                else
                {
                    ajout = false;
                }
                if(ajout)
                {
                    XtraMessageBox.Show("Commande enregistrée");
                }
                else
                {
                    XtraMessageBox.Show("Une erreur s'est produite lors de l'enregistrement de la commande");
                }
            }
            else
            {
                MessageBox.Show("Quantités insuffisantes pour certains produits");
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //recuperer User
            textEdit1.Text = User.GetUsersInformations(Convert.ToInt32(comboBox1.SelectedValue)).NOM;
            textEdit2.Text = User.GetUsersInformations(Convert.ToInt32(comboBox1.SelectedValue)).PRENOM;
            textEdit3.Text = User.GetUsersInformations(Convert.ToInt32(comboBox1.SelectedValue)).MAIL;
            memoEdit1.Text = User.GetUsersInformations(Convert.ToInt32(comboBox1.SelectedValue)).ADRESSE
                             + "\r\n" + User.GetUsersInformations(Convert.ToInt32(comboBox1.SelectedValue)).CP
                             + "\r\n" + User.GetUsersInformations(Convert.ToInt32(comboBox1.SelectedValue)).VILLE;
            textEdit4.Text = User.GetUsersInformations(Convert.ToInt32(comboBox1.SelectedValue)).VILLE;

        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}